<?php

namespace App\Admin\Controllers;

use App\Models\Ad;

use App\Models\AdPositionId;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class AdController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('广告');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Ad::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->ad_position()->name("广告位");
            $grid->column('cover','图片')->thumb(40,40);
            $grid->column('title','标题');
            $grid->column('describe','描述');
            $grid->column('href','链接');
            $grid->column('check_login','需要登录');
            $grid->column('new_open','新窗口打开');
            $grid->column('order','排序');
            $grid->column('event','事件')->type_name(Ad::EVENT)->label();
            $grid->column('state','状态')->switch();
            $grid->column('created_at','创建时间');
            $grid->column('updated_at','更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Ad::class, function (Form $form) {

            $form->select('ad_position_id','广告位')->options(AdPositionId::all()->pluck('name','id'));
            $form->text('title','标题');
            $form->text('describe','描述');
            $form->image('cover','图片')->uniqueName();
            $form->text('href','链接')->help("<span class='label label-success'>{url}</span>为小程序网页链接前缀，会解析成".config('wx.web_host'));
            $form->switch('check_login','需要登录');
            $form->switch('new_open','新窗口打开');
            $form->switch('state','状态');
            $form->number('order','排序');
            $form->select('event','事件')->options(Ad::EVENT);
        });
    }
}
